package it.fuesi.expired.formbinders;

import android.app.Activity;
import android.content.Intent;

import it.fuesi.expired.api.client.base.ACrudBroker;
import it.fuesi.expired.model.base.AModel;
import it.fuesi.expired.model.base.ErrorModel;
import it.fuesi.expired.model.base.ModelCollection;

/**
 * Created by pascotto on 22/08/14.
 */
public abstract class AbstractFormBinder<T extends AModel> {

    protected static final String TAG = "AbstractFormBinder";

    protected abstract ACrudBroker getClientBroker();

    public abstract AModel makeObject(Activity activity);

    public abstract void bindObject(Activity activity, T obj);

    public abstract void onCreate(Activity activity);

    public T save(T obj){
        final ACrudBroker<T,ModelCollection<T>> broker = getClientBroker(); //FIXME: only one instance please

        if(obj.get_id() != null)
            return broker.update(obj);
        else
            return broker.add(obj);
    }

    public T get(String id){
        ACrudBroker<T,ModelCollection<T>> broker = getClientBroker(); //FIXME: only one instance please
        return broker.get(id);
    }

    public ErrorModel delete(String id){
        final ACrudBroker<T,ModelCollection<T>> broker = getClientBroker(); //FIXME: only one instance please

        return broker.delete(id);
    }

    public void returnDate(String formattedDate){
        //NOTHING TO DO
    }

    public abstract void onActivityResult(int requestCode, int resultCode, Intent data);


}
